<?php 
require_once('../../libraryfiles/config.php');
  session_start();
if(isset($_POST['submit'])){
$allowedExts = array("jpg", "jpeg", "gif", "png");
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))) {
    if (file_exists("uploads/" . $_FILES["file"]["name"]))
      {
      $_SESSION['mg_same']= "<p>".$_FILES["file"]["name"] . " already exists. </p>";
	  header("location:index.php");
      }
    else{
		 if($_FILES['file']['name']!=""){
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "uploads/" . $_FILES["file"]["name"]);
        $filename=$_FILES['file']['name'];
		$select_update="SELECT *FROM tbl_bg_image WHERE bg_status=1";
		$run_select=mysql_query($select_update);
		$sel_assoc=mysql_fetch_assoc($run_select);
		$status= $sel_assoc["bg_status"];
		$_SESSION['id']=$sel_assoc["bg_id"];
		$image_name=$sel_assoc['bg_nam'];
		$updat_image="UPDATE tbl_bg_image SET bg_nam='".$filename."' WHERE bg_status='".$status."'";
		mysql_query($updat_image);
		if($image_name){
			
			 unlink("uploads/".$image_name);
			}
			 header("location:index.php");
		
      }
	}
  }
else
  {
  $_SESSION['ms_invalid']= "<p>Invalid File and Field is required!</p>";
  header("location:index.php");
  }
}

?>